package com.jason.canvas.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;

/**
 * @DESC:
 *      Region和Rect区别:
 *          1.Region表示的是一个区域,而Rect表示的是一个矩形(最根本的区别)
 *          2.Region不受Canvas的变换影响,Canvas的local不会直接影响到Region的自身
 *
 * @Author: Jason
 * @Date: 16/9/26
 * @Time: 11:19
 */

public class ClipRegionView15 extends View {

    private Region mRegion; // 区域对象
    private Rect mRect; // 矩形
    private Paint mPaint; // 绘制边框画笔

    public ClipRegionView15(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.DKGRAY);
        mPaint.setStrokeWidth(2);

        // 实例化矩形
        mRect = new Rect(0, 0, 200, 200);

        mRegion = new Region(200, 200, 400, 400);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        // 缩放画布
        canvas.scale(0.75F, 0.75F); // 画布缩放 rect也跟着缩放 Region并没有缩放

        canvas.save();

        // 裁剪矩形
        canvas.clipRect(mRect);
        canvas.drawColor(Color.RED);

        canvas.restore();

        canvas.save();

        // 裁剪区域
        canvas.clipRegion(mRegion);
        canvas.drawColor(Color.RED);
        canvas.restore();

        // 为画布创建边框
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mPaint);
    }
}
